package com.szewec.ps.myview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.content.ContextCompat;

import com.szewec.ps.R;
import com.szewec.ps.util.StringUtils;

public class HigherInputItem extends ConstraintLayout implements BaseInput {
    protected TextView itemName;
    protected EditText itemValue;
    private CharSequence hint;

    //滑动距离的最大边界
    private int mOffsetHeight;

    //是否到顶或者到底的标志
    private boolean mBottomFlag = false;
    private boolean mCanVerticalScroll;

    public HigherInputItem(Context context) {
        super(context);
        init(context,null);
    }

    public HigherInputItem(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context,attrs);
    }

    public HigherInputItem(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context,attrs);
    }

    private void init(Context context, AttributeSet attrs){
        LayoutInflater.from(context).inflate(R.layout.higher_input_item_view, this);
        itemName = findViewById(R.id.tv_item_name);
        itemValue = findViewById(R.id.et_item_value);

        parseStyle(context, attrs);
    }

    private void parseStyle(Context context, AttributeSet attrs){
        if(attrs != null){
            TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.InputItem);
            String name = ta.getString(R.styleable.InputItem_itemName);
            itemName.setText(name);
            String h = ta.getString(R.styleable.InputItem_itemHint);
            //记录原始hint
            hint = h;
            if (StringUtils.isEmpty(h)){
                hint = "请输入";
            }
            itemValue.setHint(hint);

            boolean required = ta.getBoolean(R.styleable.InputItem_required,false);

            if (!required){
                itemName.setCompoundDrawables(null,null,null,null);
            }

            ta.recycle();
        }
    }

    public String getStringValue(){
        if(itemValue.getText() == null || StringUtils.isEmpty(itemValue.getText().toString())){
            return null;
        }else {
            String value = itemValue.getText().toString();
            return value;
        }
    }
    public Double getDoubleValue(){
        if(itemValue.getText() == null){
            return null;
        }else {
            Double value = Double.valueOf(itemValue.getText().toString());
            return value;
        }
    }
    public Integer getIntValue(){
        if(itemValue.getText() == null){
            return null;
        }else {
            Integer value = Integer.valueOf(itemValue.getText().toString());
            return value;
        }
    }

    public void setValue(Object value){
        if (value == null){
            return;
        }
        itemValue.setText(value.toString());
    }

    /**
     * 设置是否必填
     * @param context
     * @param required
     */
    public void setRequired(Context context,boolean required){
        if (required){
            Drawable drawable = ContextCompat.getDrawable(context, R.drawable.red_star);
            itemName.setCompoundDrawablesWithIntrinsicBounds(drawable,null,null,null);
        }else {
            itemName.setCompoundDrawablesWithIntrinsicBounds(null,null,null,null);
        }
    }

    /**
     * textChange事件
     * @param watcher
     */
    public void setTextChangeListener(TextWatcher watcher){
        if (watcher == null){
            return;
        }
        itemValue.addTextChangedListener(watcher);
    }

    public TextView getEditTextView(){
        return itemValue;
    }

    /**
     * 切换textview
     *
     */
    @Override
    public void switchoverMode(boolean isEnabled) {
        if (isEnabled) {
            itemValue.setEnabled(true);
            itemValue.setHint(hint);
        } else {
            itemValue.setEnabled(false);
            itemValue.setHint("");
            ViewGroup.LayoutParams params = itemValue.getLayoutParams();
            params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
            itemValue.setLayoutParams(params);
        }
    }


    /**
     * 解决组件滚动问题
     * @param ev
     * @return
     */
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                if(itemValue.hasFocus()){
                    getParent().requestDisallowInterceptTouchEvent(true);
                }
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                getParent().requestDisallowInterceptTouchEvent(false);
                break;
        }
        return super.dispatchTouchEvent(ev);
    }

    public void setItemName(String itemName){
        this.itemName.setText(itemName);
    }
}
